package cn.kgc.base.controller;

import cn.kgc.base.service.FileService;
import cn.kgc.commons.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p></p>
 *
 * @author 李孟鸠
 * @Date: 2022年11月29日8:28
 */
@RestController
@RequestMapping("/oss")
@Api
@ConditionalOnClass(ResponseBodyAdvice.class)
public class OssFileController {

    @Resource(name = "ossFileService")
    private FileService fileService;

    @RequestMapping("/upload")
    @ApiOperation(value = "oss的文件上传",httpMethod = "POST",
            response = Result.class,produces = "application/json;charset=UTF-8")
    public Result upload(@RequestParam("file") MultipartFile file,
                         HttpServletRequest request){
        //todo 从请求中获得用户的ID
        String userId="0";
        String path = fileService.upload(file, userId, file.getOriginalFilename());
        return Result.ok(path,request.getRequestURL().toString());
    }
}
